package com.hxk.juc.thread.syn8d;

import java.util.concurrent.TimeUnit;

/***
 *  //结论：因为ｍ２和ｍ３都是static方法，在static方法上加synchronized锁的是当前对象的class对象，
 //所以ｍ３会受ｍ２的影响，必须等ｍ２执行完后在执行，ｍ２和ｍ３不会受ｍ１的影响，因为ｍ１锁的是当前对象,
 // m4因为锁的是新new的ｏｂｊ对象，所以他不受ｍ１，ｍ２，ｍ３三个方法的影响
 */
public class SynchronizedCase4 {

    public synchronized void m1(){
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("m1");
    }

    public synchronized static void m2(){
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("m2");
    }

    public synchronized static void m3(){
        System.out.println("m3");
    }

    private static  final Object obj = new Object();
    public static void m4(){
        synchronized(obj){
            System.out.println("m4");
        }
    }

    public static void main(String[] args) {

        SynchronizedCase4 case4 = new SynchronizedCase4();

        new Thread(()->{case4.m1();},"001").start();

        new Thread(()->{
            SynchronizedCase4.m2();},"002").start();

        new Thread(()->{
            SynchronizedCase4.m3();},"003").start();

        new Thread(()->{
            SynchronizedCase4.m4();},"004").start();


    }
}
